package com.toy.abash.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.toy.abash.domain.Abash;
import com.toy.abash.service.AbashManager;
import com.toy.base.domain.User;
import com.toy.base.service.UserManager;
import com.toy.core.client.SystemFacade;
import com.toy.core.dao.support.Page;
import com.toy.core.web.BaseController;

@Controller
@RequestMapping("/abash")
public class HomeController extends BaseController {
	
	@Autowired
	private AbashManager abashManager;
	
	@Autowired
	private UserManager userManager;
	
	public static String  APPS_PREFIX = "/abash";
	
	//每日/周/年/所有最热
	@RequestMapping("/{hottype}/{pageNo}")
	public String showDayHot(@PathVariable String hottype,
			@PathVariable int pageNo,Model model){
		Page<Abash> page = abashManager.findHotPostsPaged(hottype,pageNo, 10);
		Map<Long,User> users = abashManager.findPostsUsers(page.getData());
		String username = SystemFacade.getSecurityService().getCurrentUsername();
		User user = userManager.getUserByUsername(username);
		model.addAttribute("page", page);
		model.addAttribute("users", users);
		model.addAttribute("user", user);
		model.addAttribute("formAction", "/abash/"+hottype);
		return APPS_PREFIX + "/home";
	}
	
	//有图有真相
	@RequestMapping("/imagehot/{pageNo}")
	public String showImageHot(@PathVariable int pageNo,Model model){
		Page<Abash> page = abashManager.findHotImagesPaged(pageNo, 10);
		Map<Long,User> users = abashManager.findPostsUsers(page.getData());
		model.addAttribute("page", page);
		model.addAttribute("users", users);
		model.addAttribute("formAction", "/abash/imagehot");
		return APPS_PREFIX + "/home";
	}
	
	//最新糗事
	@RequestMapping("/newposts/{pageNo}")
	public String showNewPosts(@PathVariable int pageNo,Model model){
		Page<Abash> page = abashManager.findNewPostsPaged(pageNo, 10);
		Map<Long,User> users = abashManager.findPostsUsers(page.getData());
		model.addAttribute("page", page);
		model.addAttribute("users", users);
		model.addAttribute("formAction", "/abash/newposts");
		return APPS_PREFIX + "/home";
	}
}
